<?php
namespace Home\Controller;
use Think\Controller;
class GoodsController extends Controller {
    public function goods(){
       $goods = D("Admin/goods");
       $gds = $goods->find(I("goods_id"));
       $gd= D('comment')->field("avg(comment_rank) as star")->where("goods_id=".I("goods_id"))->select();
      $gds['star'] =round($gd[0]['star'],0);
       $data = $this->brand($gds['cat_id']);
       // $comment = D('comment')->where("goods_id=".I("goods_id"))->select();
       //关联表chaxun
       $comment = $goods->relationGet('comment');
       foreach (array_reverse($comment) as $key => $v) {
         $comment[$key]['content'] = htmlspecialchars_decode($v['content']);
       }
       //把商品信息存入历史
       $this->hisotry($gds);
       $this->assign("goodsinfo",$gds);
       $this->assign("comment",$comment);
        $this->assign("mbx",$data);
       $this->display();
    }
    //商品历史存入session
    public function hisotry($g)
    {
      $hisotry = session("hisotry");
      if(empty($hisotry)){
        $hisotry = array();
      }
      if(isset($hisotry[$g['goods_id']])){
        unset($hisotry[$g['goods_id']]);
      }
      if($g != NULL){
       $row = array();
       $row['goods_thumb'] = $g['goods_thumb'];
       $row['goods_id'] = $g['goods_id'];
       $row['goods_name'] = $g['goods_name'];
       $row['shop_price'] = $g['shop_price'];
       $hisotry[$g['goods_id']] = $row;
     }
       // $hisotry=array_reverse($hisotry);
       // 历史不超过6个；
       if(count($hisotry) > 6){
        $key = key($hisotry);
        unset($hisotry[$key]);
       } 
      session("hisotry",$hisotry);
    }
    //加入购物车
    public function addCar()
    {
      $goodsinfo = D('Admin/goods')->find(I('goods_id'));
      $Tool =\Home\Tool\AddTool::getIns();
      $Tool->add(I('goods_id'),$goodsinfo['goods_name'],$goodsinfo['shop_price'],$goodsinfo['market_price']);
      $this->success('加入购物车成功!','',1);
    }
    /*
     *商品面包屑
     */
    public function brand($catId)
    {
      $cat = D("Admin/cat");
      while($catId >0){
      	foreach ($cat->order("cat_id ASC")->select() as $k => $v) 
      	{
          if($catId == $v['cat_id']){
          	$arr[$catId]=$v;
          	$catId=$v['parent_id'];
          }
      	}

      }
      return array_reverse($arr);
    }
    //商品评论
    public function comment()
    {
      if(IS_POST){
        if(!cookie("username")){
          $this->error('您还没有登录,请登录!',U('home/login/login'),2);
        }
        if(!I('post.content')){
          $this->error('评论内容不能为空!','',2);
        }
        $data = I('post.');
        unset($data['captcha']);
        $data['user_id'] = cookie("userid");
        // if(!D('comment')->create($data)){
        //      $msg = D('comment')->getError();
        //      $this->error($msg,'',2);
        // }
        $code = I("post.captcha");
        $Verify = new \Think\Verify(); 
        if(!$Verify->check($code)){
               $this->error("验证码错误！","",1);
        }
        if(D('comment')->add($data)){
          $this->success("评论成功！","",2);
        }
      }


    }
}